package phoenixAAD.WifiFlashDrive;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SettingsActivity extends Activity {
	
	private EditText username;
	private EditText pw1;
	private EditText pw2;
	private Button okButton;
	
	@Override	
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        username = (EditText)findViewById(R.id.username);
        pw1 = (EditText)findViewById(R.id.password1);
        pw2 = (EditText)findViewById(R.id.password2);
        okButton = (Button)findViewById(R.id.okButton);
       
        Intent i = getIntent();
        if(i.getExtras()==null)
        	createAlertDialog(R.string.setting_welcome,R.string.setting_welcome_text);
        
        okButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				String sn = username.getText().toString();
				String password = pw1.getText().toString();
				String password2 = pw2.getText().toString();
				if(!password.equals(password2)){
					//Log.d("debug","passwords don't match.");
					//alert dialog
					createAlertDialog(R.string.caution, R.string.setting_passwordmatch);
				}else if(sn.length()==0){
					//Log.d("debug","Please enter a username");
					createAlertDialog(R.string.caution, R.string.setting_nousername);
				}else if(pw1.length()==0){
					//Log.d("debug","You must enter a password");
					createAlertDialog(R.string.caution,R.string.setting_nopassword);
				}else{
					//Log.d("debug","username is: "+sn+" password is: "+password);
					Intent i = new Intent();
					Bundle b = new Bundle();
					b.putString("username", sn);
					b.putString("password", password);
					i.putExtras(b);
					SettingsActivity.this.setResult(1, i);
					SettingsActivity.this.finish();
				}
				
			}
        	
        });
    }
	public void createAlertDialog(int titleRes, int messageRes){
		String title = getString(titleRes);
		String message = getString(messageRes);
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setNegativeButton(getString(android.R.string.ok), null);
		alertDialog.show();
	}
}
